package com.ayoka.parenting.ui.live.memberlist;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.MemberListAdapter;
import com.ayoka.parenting.bean.BasicUserInfoBean;
import com.ayoka.parenting.databinding.ActivityBaseRecyclerviewBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.LiveDataBus;

import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.io.Serializable;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MemberListActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MemberListActivity extends MVPBaseRecyclerViewActivity<BasicUserInfoBean, ActivityBaseRecyclerviewBinding, MemberListContract.View, MemberListPresenter> implements MemberListContract.View {


    public static void start(Activity activity, boolean forResult, List<BasicUserInfoBean> userBeanList) {
        Intent intent = new Intent(activity, MemberListActivity.class);
        intent.putExtra("ForResult", forResult);
        intent.putExtra("BasicUserInfoBeanList", (Serializable) userBeanList);
        if (forResult) {
            activity.startActivityForResult(intent, REQUEST_CODE_FOR_RESULT);
        } else {
            activity.startActivity(intent);
        }
    }

    private List<BasicUserInfoBean> userBeanList;

    private boolean forResult;
    private int selectedIndex;

    @Override
    public void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            forResult = intent.getBooleanExtra("ForResult", false);
            userBeanList = (List<BasicUserInfoBean>) intent.getSerializableExtra("BasicUserInfoBeanList");
        }
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @Override
    protected boolean getLoadEnable() {
        return false;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<BasicUserInfoBean, BaseViewHolder> getAdapter() {
        MemberListAdapter adapter = new MemberListAdapter(forResult ? MemberListAdapter.SINGLE_CHOICE : 0);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ((MemberListAdapter) adapter).setSingleChoice(position);
                selectedIndex = position;
            }
        });
        return adapter;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        setCustomToolBar(mDataBinding.customToolbar.toolbar, new ToolBarOptions("成员列表"));
        LiveDataBus.getInstance().with("MemberList", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                adapter.notifyDataSetChanged();
            }
        });
        setLoadMoreLayout(userBeanList);
    }

    @Override
    protected MemberListPresenter getPresenter() {
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (forResult) {
            getMenuInflater().inflate(R.menu.menu_member_list, menu);//加载menu文件到布局
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        if (item.getItemId() == R.id.action_confirm) {
            if (forResult) {
                BasicUserInfoBean userBean = adapter.getItem(selectedIndex);
                Intent intent = new Intent();
                intent.putExtra("BasicUserInfoBean", userBean);
                setResult(RESULT_OK, intent);
                finish();
            }
        }
        return true;
    }
}
